$(function(){
	var tableSource = new $.jqx.dataAdapter({
		dataType: "json",
		dataFields:[
		    { name: "id", type: "int" },
		    { name: "rebate_type", type: "int" },
		   /* { name: "rebate_product", type: "string" },
		    { name: "rebate_productid", type: "string" },*/
		    { name: "rebate_shoptype", type: "string" },
		    { name: "rebate_amt", type: "float" },
		    { name: "rebate_coupon", type: "float" },
		    { name: "rebate_samt", type: "float" },
		    { name: "rebate_scoupon", type: "float" },
		    { name: "rebate_tamt", type: "float" },
		    { name: "rebate_tcoupon", type: "float" },
		    { name: "rebate_link", type: "string" },
	  	    { name: "createtime", type: "date" },
		],
		id: "id",
		url: "rebatelink/all.do"
	});
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: tableSource,
        pageable: true,
        serverProcessing: true,
        pageSize: 18,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width: '10%', cellsRenderer: function(row) { console.log(tableSource); return (row + 1).toString(); } },
            { text: "推广类型", dataField: "rebate_type", width: '10%',cellsRenderer: function(row, column, value, rowData){
        		return ["商品推广","注册推广"][value];
        	}},
           /* { text: "商品", dataField: "rebate_product", width: '5%' },
            { text: "商品id", dataField: "rebate_productid", width: '5%' },*/
            { text: "注册类型", dataField: "rebate_shoptype", width: '10%',cellsRenderer: function(row, column, value, rowData){
        		return ["商户","供应商","代理商","商品推广","普通用户"][value];
        	}},
            { text: "一级返利金额", dataField: "rebate_amt", width: '10%' },
            { text: "一级返利积分", dataField: "rebate_coupon", width: '10%' },
            { text: "返利链接", dataField: "rebate_link", width: 'auto' },
            { text: "创建时间", dataField: "createtime", width: 150,
            	cellsRenderer: function(row, column, value, rowData){
            		return format(value, "Y-M-D H:I");
            	}
            }
        ],
	});
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;
	 	_editerIndex = layer.open({
			id: "sellerEditer",
			title: "返利信息",
		    type: 1,
		    skin: 'layui-layer-rim',
		    area: ['1000px', '320px'],
		    content:
		    '<form id="rebatelinklist-editer" class="form" method="post" style="height: 100%;overflow: auto;">' +
		    '<div id="order_editer" class="form" style="overflow: auto;">' +
			'<input type="hidden" name="id" />' +
			'<input type="hidden" name="submit-url" value="rebatelink/updateRebate.do" />' +
			'<label class="from-label" for="black_type">推广类型：</label>' +
			'<select name="rebate_type" id="rebate_type" class="form-control" onchange="rebate_typeSelect(this)">' +
			'<option value="0">商品推广</option><option value="1">注册推广</option></select>' + 
			'<label class="from-label" for="rebate_product" rebate_type="product" style="display:none"></label>' +
			'<input type="text" name="rebate_productid" allowBlank="true" class="form-control" placeholder="商品" rebate_type="product" style="display:none;    visibility: hidden;"/>' +
			'<label class="from-label" for="rebate_shoptype" rebate_type="shop" style="display:none">注册类型：</label>' +
			'<select name="rebate_shoptype" id="rebate_shoptype" class="form-control" rebate_type="shop" style="display:none">' +
			'<option value="0">商户</option><option value="1">供应商</option><option value="2">代理商</option><option value="4">普通用户</option></select>' + 
			'<label class="from-label">一级返利金额：</label>' +
			'<input type="text" name="rebate_amt" allowBlank="false" class="form-control">' +
			'<label class="from-label">一级返利积分：</label>' +
			'<input type="text" name="rebate_coupon" allowBlank="false" class="form-control"/>' +
			'<label class="from-label">二级返利金额：</label>' +
			'<input type="text" name="rebate_samt" allowBlank="false" class="form-control">' +
			'<label class="from-label">二级返利积分：</label>' +
			'<input type="text" name="rebate_scoupon" allowBlank="false" class="form-control"/>' +
			'<label class="from-label">三级返利金额：</label>' +
			'<input type="text" name="rebate_tamt" allowBlank="false" class="form-control">' +
			'<label class="from-label">三级返利积分：</label>' +
			'<input type="text" name="rebate_tcoupon" allowBlank="false" class="form-control"/>' +
			'<label class="from-label">返利链接：</label>' +
			'<input type="text" name="rebate_link" allowBlank="false" class="form-control" placeholder="返利链接" />' +
			'</div>' +
			'</form>'+
			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
    		'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
    		'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this, true)" value="返回"/>' +
    		'</div>'
		});
	 	$("input[name='id']").val(editingData['id']);
	 	setData($("#rebatelinklist-editer"), editingData);
	 	rebate_typeSelect();
	});
});

/**
 * 是否选中一项
 * 
 * @returns
 */
function isSelected() {
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length < 1) {
		layer.msg("请选择一行数据", {
			shift : 6,
			offset : "5%"
		});
		return null;
	}
	return rows;
}
function rebate_typeSelect() {
	if ($("#rebate_type").val() == 0) {
		$("[rebate_type=product]").show();
		$("[rebate_type=shop]").hide();
	} else if ($("#rebate_type").val() == 1) {
		$("[rebate_type=product]").hide();
		$("[rebate_type=shop]").show();
		$("[name=rebate_product]").val("");
	}
}
